package com.lzk.pet.controller;


import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.pet.domain.Pet;
import com.lzk.pet.query.PetQuery;
import com.lzk.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/pet")

@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 上架接口
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public JsonResult onSale(@RequestBody List<Long> ids){
        try {
            return petService.onSale(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"上架失败！"+e.getMessage());
        }
    }

    /**
     * 下架接口
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            return petService.offSale(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"下架失败！"+e.getMessage());
        }
    }
    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Pet> findAll(){
        return petService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public Pet findOne(@PathVariable("id") Long id){
        return petService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param pet
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Pet pet){
        try {
            if (pet.getId() == null){
                petService.save(pet);
            }else {
                petService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param petQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery petQuery){
        return petService.queryData(petQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            petService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
